<template>
	<div class="app-container">
		<jk-operation-card ref="box1" class="margin-bottom-10">
			<div class="flex-between-center-wrap">
				<div class="flex-start-center-wrap">
					<el-radio-group v-model="comName" size="small">
						<el-radio-button label="gridView">
							<span class="el-icon-s-grid"></span>
							<span>网格</span>
						</el-radio-button>
						<el-radio-button label="listView">
							<span class="el-icon-notebook-2"></span>
							<span>列表</span>
						</el-radio-button>
					</el-radio-group>
				</div>
				<div class="flex-end-center-wrap">
					<jk-select v-model="searchParams.workshopId" :option-list="workshopList" :clearable="false" placeholder="区域" class="search-bar-item-size1 margin-5-0 item-gap"></jk-select>
					<jk-search-input v-model="searchParams.nameOrCode	" placeholder="设备编号或名称" size="small" class="search-bar-item-size2 margin-5-0 item-gap" clearable @clear="onSearchBtn" @keyup.enter.native="onSearchBtn" />
					<jk-search-button @click="onSearchBtn"></jk-search-button>
				</div>
			</div>
		</jk-operation-card>

		<jk-card>
			<component
				:is="comName"
				:table-height="tableHeight"
				:table-column-list="tableColumnList"
				:data-list="tableData"
				@on-call="onCallBtn"
			></component>
			<!-- 分页组件 -->
			<footer ref="box2" class="flex-end-center margin-top-10">
				<jk-page
					:page-index="searchParams.pageNo"
					:page-sizes="searchParams.pageSizes"
					:page-size="searchParams.pageSize"
					:total="totalCount"
					@on-change="onPageChange"
				/>
			</footer>
		</jk-card>
		<call-dialog
			:data-item="dataItem"
			:dialog-state="callDialogState"
			@on-visible-change="onVisibleChange"
			@on-finish="onFinish"
		></call-dialog>
	</div>
</template>

<script>
    import callDialog from './call-dialog';
    import calHeight from '/src/mixins/cal-table-height';
    import { pageConfig } from '/src/utils/jk/jk-tool';
    import getMenuColumn from '../../../../mixins/getMenuColumn';
    import handlerCache from '../../../../mixins/handler-cache';
    import { auditStateList } from '../../../../utils/jk/status-option-list';
    import { machinemanagerMachinePage } from '../../../../api/machine/machineArchives';
    import listView from './components/list-view';
    import gridView from './components/grid-view';
    export default {
        name: 'FaultCall',
        components: {
            gridView,
            listView,
            callDialog
        },
        mixins: [calHeight, getMenuColumn, handlerCache],
        data() {
            return {
                prefix: '',
                comName: 'gridView',
                apiItem: {
                    'page': machinemanagerMachinePage
                },
                workshopList: [],
                auditStateList: auditStateList,
                dataItem: null,
                callDialogState: false,
                tableLoading: true,
                tableData: [],
                totalCount: 0,
                searchParams: {
                    pageNo: 1,
                    pageSize: pageConfig.pageSize,
                    nameOrCode: '',
                    workshopId: null,
                    auditState: null
                }
            };
        },
        methods: {
            onPageChange(pageIndex, pageSize) {
                this.searchParams.pageNo = pageIndex;
                this.searchParams.pageSize = pageSize;
                this.getListRequest();
            },
            onFinish() {
                this.callDialogState = false;
                this.getListRequest();
            },
            onVisibleChange(e) {
                if (!e) {
                    this.dataItem = null;
                }
                this.callDialogState = e;
            },
            /** 查询列表 */
            getListRequest() {
                this.tableLoading = true;
                this.apiItem.page(this.searchParams).then(res => {
                    this.tableLoading = false;
                    if (res.data.status === 200) {
                        this.tableData = res.data.res;
                        this.totalCount = res.data.count;
                    }
                });
            },
            /** 搜索按钮操作 */
            onSearchBtn() {
                this.searchParams.pageNo = 1;
                this.totalCount = 1;
                this.getListRequest();
            },
            onCallBtn(row) {
                this.dataItem = {
                    workshopId: row.workshopId,
                    workshopCode: row.workshopCode,
                    workshopName: row.workshopName,

                    processId: row.processId,
                    processCode: row.processCode,
                    processName: row.processName,

                    machineId: row.id,
                    machineCode: row.code,
                    machineName: row.name
                };
                this.callDialogState = true;
            },
            clearCacheMethod() {
                // Object.assign(this.$data, this.$options.data());
                this.$nextTick(() => {
                    this.getDependentData();
                });
            },
            async getDependentData() {
                this.workshopList = this.$store.state.user.userWorkshop.workshops;
                await this.getMenuColumnListRequest();
                await this.getListRequest();
            }
        },
        mounted() {
            this.$set(this.searchParams, 'workshopId', this.$store.state.user.userWorkshop.defaultWorkshopId);
            this.getDependentData();
        }
    };
</script>
